/**
 * @(#)Main.java 2011-8-24 Copyright 2011 it.kedacom.com, Inc. All rights
 *               reserved.
 */

package com.kedacom.xmpp.demo.oneserver.chat;

import java.util.Collection;

import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;



 /**
  * (用一句话描述类的主要功能)
  * @author 何霄刚  
  * @date 2011-12-20
  */
 
public class User2Client {

	/**
	 * (用一句话描述方法的主要功能)
	 * @param args
	 * @throws XMPPException
	 * @throws InterruptedException
	 */

	public static void main(String[] args) throws XMPPException, InterruptedException {
		String SERVER = "kedacom.com", RESOURCE = "java", USER = "kongchun", PASS = "888";
		Connection conn = null;

		// 登录
		conn = new XMPPConnection(SERVER);
		conn.connect();
		conn.login(USER, PASS, RESOURCE);
		System.out.println("kongchun已登录!");
		// 设置出席状态－在线
		Presence p = new Presence(Presence.Type.available);
		p.setStatus("在线");
		conn.sendPacket(p);

		// 获取我的花名册
		final Roster roster = conn.getRoster();
		System.out.println("=======================我的花名册：=======================");
		for (RosterEntry rosterEntry : roster.getEntries()) {
			System.out.print("name: " + rosterEntry.getName() + ",jid: " + rosterEntry.getUser() + ",subscription: " + rosterEntry.getType().toString());
			System.out.println("");
		}
		System.out.println("=======================我的花名册：=======================");
		
		// 监听联系人出席状态
		roster.addRosterListener(new RosterListener() {

			@Override
			public void entriesAdded(Collection<String> arg0) {
				for(String s : arg0){
					System.out.println("Entries Added: " + s);
				}
			}

			@Override
			public void entriesDeleted(Collection<String> arg0) {
				for(String s : arg0){
					System.out.println("Entries Deleted: " + s);
				}
			}

			@Override
			public void entriesUpdated(Collection<String> arg0) {
				for(String s : arg0){
					System.out.println("Entries Updated: " + s);
				}
			}

			@Override
			public void presenceChanged(Presence presence) {
				System.out.println("Presence changed: " + presence);
			}
		});
		
		// 创建分组
		String groupName = "同事";
		if(roster.getGroup(groupName)==null){
			roster.createGroup(groupName);
		}
		// 添加联系人
		String contactName = "hexiaogang@kedacom.com";
		RosterEntry entry = roster.getEntry(contactName);
		if(entry!=null){
			roster.removeEntry(entry);
		}
		roster.createEntry(contactName, "何霄刚", new String[]{"同事"});
		
		// 改变出席状态
		for(int i=0;i<3;i++){
			Thread.currentThread().sleep(2000);
			// 改变出席状态－离线
			p.setMode(Presence.Mode.away);
			p.setStatus("离开");
			conn.sendPacket(p);
			Thread.currentThread().sleep(2000);
			// 改变出席状态－在线
			p.setMode(Presence.Mode.available);
			p.setStatus("在线");
			conn.sendPacket(p);
		}
		
		conn.getChatManager().addChatListener(new ChatManagerListener() {

			public void chatCreated(org.jivesoftware.smack.Chat chat, boolean createdLocally) {
				chat.addMessageListener(new MessageListener() {

					public void processMessage(org.jivesoftware.smack.Chat chat, Message message) {
						String messageBody = message.getBody();
						PacketExtension pe;

						pe = message.getExtension("composing", "http://jabber.org/protocol/chatstates");
						if (pe != null) {
							System.out.println("对方正在输入......");
							return;
						}

						pe = message.getExtension("active", "http://jabber.org/protocol/chatstates");
						if (pe != null) {
							System.out.println("接收到信息：" + messageBody);
							return;
						}

						pe = message.getExtension("paused", "http://jabber.org/protocol/chatstates");
						if (pe != null) {
							System.out.println("对方已暂停输入");
							return;
						}

						pe = message.getExtension("inactive", "http://jabber.org/protocol/chatstates");
						if (pe != null) {
							System.out.println("对方聊天窗口失去焦点");
							return;
						}

						pe = message.getExtension("gone", "http://jabber.org/protocol/chatstates");
						if (pe != null) {
							System.out.println("对方聊天窗口被关闭");
							return;
						}

						System.out.println("接收到信息：" + messageBody);

					}
				});
			}

		});

		Thread.currentThread().suspend();
	}

}
